package com.kolon.business.mBoard.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mBoard.controller.MBoardListController")
public class MBoardListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mBoard/mBoardList.do")
	public ModelAndView controller(HttpServletRequest request,
			
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String UserCompanyCode = (String)userMap.get("CompanyCD");
		// 회사게시판
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/board.aspx?strMode=").append("2");
		buffer.append("&UserCompanyCode=").append(UserCompanyCode);
		buffer.append("&BoardRootCD=").append("1");
		buffer.append("&ParentCD=").append("-1");
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren();
		Iterator itXml = xmlList.iterator();
		List mapList = new ArrayList();
		Map map = new HashMap();
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("BoardRootCD", el.getChild("BoardRootCD").getText());
			map.put("BoardCD", el.getChild("BoardCD").getText());
			map.put("ParentCD", el.getChild("ParentCD").getText());
			map.put("Depth", el.getChild("Depth").getText());
			map.put("BoardName", el.getChild("BoardName").getText());
			map.put("BoardNameUtf", ComUtil.getEncode(el.getChild("BoardName").getText(),"UTF-8"));
			map.put("B_IsSub", el.getChild("B_IsSub").getText());
			map.put("B_ACL", el.getChild("B_ACL").getText());
			map.put("SAdmin", el.getChild("SAdmin").getText());
			map.put("Password", el.getChild("Password").getText());
			mapList.add(map);
		}
		// 부서게시판
		StringBuffer buffer2 = new StringBuffer();
		buffer2.append(url);
		buffer2.append("/board.aspx?strMode=").append("2");
		buffer2.append("&UserCompanyCode=").append(UserCompanyCode);
		buffer2.append("&BoardRootCD=").append("2");
		buffer2.append("&ParentCD=").append("-1");
		buffer2.append("&userid=").append(id);
		buffer2.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc2 = ComUtil.getDocument(buffer2.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren();
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		// XML 데이터 
		while (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map = new HashMap();
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("BoardRootCD", el.getChild("BoardRootCD").getText());
			map.put("BoardCD", el.getChild("BoardCD").getText());
			map.put("ParentCD", el.getChild("ParentCD").getText());
			map.put("Depth", el.getChild("Depth").getText());
			map.put("BoardName", el.getChild("BoardName").getText());
			map.put("BoardNameUtf", ComUtil.getEncode(el.getChild("BoardName").getText(),"UTF-8"));
			map.put("B_IsSub", el.getChild("B_IsSub").getText());
			map.put("B_ACL", el.getChild("B_ACL").getText());
			map.put("SAdmin", el.getChild("SAdmin").getText());
			map.put("Password", el.getChild("Password").getText());
			mapList2.add(map);
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("mapList2", mapList2);
		modelAndView.addObject("tog", request.getParameter("tog"));
		modelAndView.addObject("BoardRootCDName1", ComUtil.getEncode("회사게시판","UTF-8"));
		modelAndView.addObject("BoardRootCDName2", ComUtil.getEncode("부서게시판","UTF-8"));
		modelAndView.addObject("CompanyCode", UserCompanyCode);
		return modelAndView;
	}

}